/**
 * 
 */
package com.dsteiner.xelavos.client;

import com.dsteiner.widgets.client.JsUtil;
import com.dsteiner.widgets.client.PopupOverlayWidget;
import com.dsteiner.widgets.client.Widgets.HTMLExt;
import com.dsteiner.widgets.client.Widgets.ImageExt;
import com.dsteiner.widgets.client.Widgets.LazyWidget;
import com.dsteiner.widgets.client.event.EventManagerImpl;
import com.dsteiner.widgets.client.model.PopupOverlayModel;
import com.dsteiner.widgets.client.resources.WidgetsImageBundle;
import com.dsteiner.xelavos.client.controller.CommentController;
import com.dsteiner.xelavos.client.model.AuthenticationModel;
import com.dsteiner.xelavos.client.model.CommentModel;
import com.dsteiner.xelavos.client.model.DisplayModel;
import com.dsteiner.xelavos.client.resources.XelavosResources;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Widget;

public class CommentWidget extends LazyWidget {
	private CommentController commentController = new CommentController();
	private final CommentModel commentModel;
	private final AuthenticationModel authenticationModel;

	public CommentWidget(CommentModel commentModel, AuthenticationModel authenticationModel) {
		this.commentModel = commentModel;
		this.authenticationModel = authenticationModel;
	}

	@Override
	public Widget createContentWidget() {
		HorizontalPanel mainHorizontalPanel = new HorizontalPanel();
		FlowPanel flowPanel = new FlowPanel();
		HTMLExt commentMessageHtml = new HTMLExt();
		HTMLExt userHtml = new HTMLExt();
		HTMLExt registeredDateHtml = new HTMLExt();
		HorizontalPanel firstLineHorizontalPanel = new HorizontalPanel();
		HorizontalPanel ratingHorizontalPanel = new HorizontalPanel();
		ImageExt ratingDownIcon = new ImageExt(WidgetsImageBundle.Util.getInstance().ratingDownIcon().createImage());
		ImageExt ratingUpIcon = new ImageExt(WidgetsImageBundle.Util.getInstance().ratingUpIcon().createImage());
		ratingHorizontalPanel.setSpacing(5);
		firstLineHorizontalPanel.setSpacing(5);
		HTMLExt ratingHtml = new HTMLExt();
		HTMLExt messageHtml = new HTMLExt();
		FlowPanel rightFlowPanel = new FlowPanel();
		PopupOverlayModel userPopupOverlayModel = new PopupOverlayModel();
		PopupOverlayWidget userPopupOverlayWidget = new PopupOverlayWidget(userPopupOverlayModel);
		DisplayModel userDisplayModel = new DisplayModel();
		DisplayWidget userDisplayWidget = new DisplayWidget(userDisplayModel, authenticationModel);
		userPopupOverlayModel.setContentWidget(userDisplayWidget);
		
		ratingHorizontalPanel.add(ratingHtml);
		ratingHorizontalPanel.add(ratingDownIcon);
		ratingHorizontalPanel.add(ratingUpIcon);
		
		firstLineHorizontalPanel.add(userHtml);
		firstLineHorizontalPanel.add(registeredDateHtml);
		
		flowPanel.add(firstLineHorizontalPanel);
		flowPanel.add(commentMessageHtml);
		
		rightFlowPanel.add(ratingHorizontalPanel);
		rightFlowPanel.add(messageHtml);
		
		mainHorizontalPanel.add(flowPanel);
		mainHorizontalPanel.add(rightFlowPanel);
		
		mainHorizontalPanel.setCellHorizontalAlignment(rightFlowPanel, HasHorizontalAlignment.ALIGN_RIGHT);
		mainHorizontalPanel.setWidth("100%");
		setStyleName(XelavosResources.INSTANCE.css().commentWidget());

		commentController.registerCommentWidget(this);
		commentController.registerCommentModel(commentModel);
		commentController.registerUserHtml(userHtml);
		commentController.registerJsUtil(JsUtil.getInstance());
		commentController.registerUserDisplayModel(userDisplayModel);
		commentController.registerUserPopupOverlayModel(userPopupOverlayModel);
		commentController.registerCommentMessageHtml(commentMessageHtml);
		commentController.registerMessageHtml(messageHtml);
		commentController.registerRegisteredDateHtml(registeredDateHtml);
		commentController.registerRatingHtml(ratingHtml);
		commentController.registerRatingDownIcon(ratingDownIcon);
		commentController.registerRatingUpIcon(ratingUpIcon);
		commentController.registerEventManager(EventManagerImpl.getInstance());
		commentController.registerAuthenticationModel(authenticationModel);
		
		return mainHorizontalPanel;
	}
}